<template>
    <div>
        <ly-dialog v-model="dialogVisible" :title="loadingTitle" width="560px" :before-close="handleClose">
            <el-form :inline="false" :model="formData" :rules="rules" ref="rulesForm" label-position="right" label-width="auto">
                <el-form-item label="应用名称：" prop="name">
                    <el-input v-model="formData.name"></el-input>
                </el-form-item>
                <el-form-item label="应用版本：" prop="ver">
                    <el-input v-model="formData.ver"></el-input>
                </el-form-item>
                <el-form-item label="备注：" prop="description">
                    <el-input v-model="formData.description" ></el-input>
                </el-form-item>
                <el-form-item label="部署脚本：" >

                </el-form-item>
                <aceEditor ref="changelang"  @on-change-language="handleChangeLanguage" v-model:value="formData.deployment" @update:value="formData.deployment = $event" ></aceEditor>
            </el-form>
            <template #footer>
                <el-button @click="handleClose" :loading="loadingSave">取消</el-button>
                <el-button type="primary" @click="submitData" :loading="loadingSave">确定</el-button>
            </template>
        </ly-dialog>
    </div>
</template>

<script>
    import {applicationDeployAdd,applicationDeployEdit} from "@/api/api";
    import LyDialog from "../../../../components/dialog/dialog";
    import aceEditor from '@/components/editor/aceEditor'
    export default {
        name: "addModuleAppserver",
        components: {LyDialog, aceEditor},
        emits: ['refreshData'],
        data() {
            return {
                dialogVisible:false,
                loadingSave:false,
                loadingTitle:'',
                formData:{
                    name: '',
                    ver:'',
                    deployment: '',
                    description:'',
                },

                rules:{
                    name: [
                        {required: true, message: '请输入应用名称',trigger: 'blur'}
                    ],
                    ver: [
                        {required: true, message: '请输入应用版本',trigger: 'blur'}
                    ],
                    // nickname: [
                    //     {required: true, message: '请输入昵称',trigger: 'blur'}
                    // ],
                },
            }
        },
        methods:{
            handleClose() {
                this.dialogVisible=false
                this.loadingSave=false
                this.formData = {
                    name: '',
                    ver:'',
                    deployment: '',
                    description:''
                }
                this.$emit('refreshData')
            },
            addUserFn(item,flag) {
                this.loadingTitle=flag
                this.dialogVisible=true
                if(item){
                    this.formData = item
                }
                // this.$refs.changelang.changeLanguage("shell")
            },
            handleChangeLanguage(changeLanguageFunc) {
                // 确保 changeLanguageFunc 不是 undefined
                if (typeof changeLanguageFunc === 'function') {
                    // 调用从事件中传递过来的方法
                    changeLanguageFunc('sh'); // 示例：切换到 Python 模式
                }
            },
            submitData() {
                this.$refs['rulesForm'].validate(obj=>{
                    if(obj) {
                        this.loadingSave=true
                        let param = {
                            ...this.formData
                        }
                        if(this.formData.id){
                            applicationDeployEdit(param).then(res=>{
                                this.loadingSave=false
                                if(res.code ==2000) {
                                    this.$message.success(res.msg)
                                    this.handleClose()
                                    this.$emit('refreshData')
                                } else {
                                    this.$message.warning(res.msg)
                                }
                            })
                        }else{
                            applicationDeployAdd(param).then(res=>{
                                this.loadingSave=false
                                if(res.code ==2000) {
                                    this.$message.success(res.msg)
                                    this.handleClose()
                                    this.$emit('refreshData')
                                } else {
                                    this.$message.warning(res.msg)
                                }
                            })
                        }

                    }
                })
            },


        }
    }
</script>

